package fr.pascal.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootPanel;

import fr.pascal.client.mvp.AppPlaceHistoryMapper;
import fr.pascal.client.mvp.DetailActivityMapper;
import fr.pascal.client.mvp.ListActivityMapper;
import fr.pascal.client.place.ListPlace;
import fr.pascal.client.ui.MainView;
import fr.pascal.client.ui.MainViewImpl;

public class Gwt_04 implements EntryPoint {

	@Override
	public void onModuleLoad() {
		
		MainView mainView = new MainViewImpl();
		RootPanel.get("mainView").add(mainView);
		
		ClientFactory clientFactory = GWT.create(ClientFactory.class);
        EventBus eventBus = clientFactory.getEventBus();
        PlaceController placeController = clientFactory.getPlaceController();
		
        // Start ActivityManager for the main widget with our ActivityMapper
        ActivityMapper listActivityMapper = new ListActivityMapper(clientFactory);
        ActivityManager listActivityManager = new ActivityManager(listActivityMapper, eventBus);
        listActivityManager.setDisplay(mainView.getListContainer());
        
        ActivityMapper detailActivityMapper = new DetailActivityMapper(clientFactory);
        ActivityManager detailActivityManager = new ActivityManager(detailActivityMapper, eventBus);
        detailActivityManager.setDisplay(mainView.getDetailContainer());
		
        // Start PlaceHistoryHandler with our PlaceHistoryMapper
        AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
        PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
        historyHandler.register(placeController, eventBus, new ListPlace());
		
        // Goes to the place represented on URL else default place
        historyHandler.handleCurrentHistory();
	}
}
